<?php
/**
 * 公司部门模型
 * @date    2022-07-01
 * @author  kiro
 * @email   294843009@qq.com
 * @version 1.0
 */
/**
rights Reserved, Designed By www.youyacao.com
@author:成都市一颗优雅草科技有限公司
@version 优雅草蜻蜓系统-服务端部分
注意：服务端代码在获得授权之前通过其他非官方渠道获得代码均为侵权，禁止用于商业用途，否则将承担因此带来等版权纠纷。
需要商业用途或者定制开发等可访问songshu.youyacao.com   联系QQ:2853810243 422108995 23625059584
优雅草开源代码地址：https://gitee.com/youyacao  可查阅所有开源项目



 */
namespace App\Models;

class CompanyDepartment extends BaseModel
{
    protected $table = 'company_department';

    protected $fillable = [
        'company_id',
        'name',
        'level',
        'pid',
        'sort',
        'status'
    ];

    public static function getList($company_id)
    {
        $list = self::where('company_id', $company_id)
            ->where('status', 1)
            ->where('pid', 0)
            ->orderBy('sort', 'ASC')
            ->orderBy('id', 'DESC')
            ->get()
            ->toArray();
        foreach ($list as &$value) {
            $value['children'] = self::getChild($company_id, $value['id']);
            $value['friendList'] = User::getList($value['id']);
        }
        return $list;
    }

    public static function getChild($company_id, $pid)
    {
        $children = self::where('company_id', $company_id)
            ->where('status', 1)
            ->where('pid', $pid)
            ->orderBy('sort', 'ASC')
            ->orderBy('id', 'DESC')
            ->get()
            ->toArray();
        foreach ($children as &$value) {
            $value['children'] = self::getChild($company_id, $value['id']);
            $value['friendList'] = User::getList($value['id']);
        }
        return $children;
    }
}
